package edu.mit.media.fv.aqview
{
	import edu.mit.media.fv.aqwidgets.*;
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.text.*;
	
	public class AQPollViewer extends AQViewer
	{
		var _question:AQPollQuestion;
		var _tags:AQTextField;
		var _sender:AQTextField;
		var _date:AQTextField;
		var _switchButton:DisplayObject;
		var _sendButton:DisplayObject;
		var _descripters:Array;
		var _goodButton:DisplayObject;
		var _badButton:DisplayObject;
		
		private var isViewingDescription:Boolean = true;
		private var choices:Array;//array of options?
		private var viewport:Sprite;
		private var scroll:FVGlassScrollPane;
		//private var optionInFocus:AQPollOption;
		private var type:int = -1;
		public function AQPollViewer()
		{
			super();
			this.scroll = new FVGlassScrollPane(10,100,600,280);
			this.scroll.background = true;
			FluidEventHandler.addEventListener(DisplayEvent.OPEN_POLL, displayWidget);
			
			
		}
		public function switchView(event:MouseEvent):void{
			var i:int = 0;
			if(isViewingDescription){
				for(i = 0; i<this._descripters.length; i++){
					this.removeChild(DisplayObject(this._descripters[i]));
				}
				this.addChild(this.scroll);
			}else{
				for(i = 0; i<this._descripters.length; i++){
					this.addChild(DisplayObject(this._descripters[i]));
				}
				this.removeChild(this.scroll);
			}
			isViewingDescription = !isViewingDescription;
		}
		
		public function displayWidget(event:DisplayEvent):void{
			FluidEventHandler.dispatchEvent(new WidgetEvent(WidgetEvent.DISPLAY_WIDGET, this, "Poll"));
			
			var i:int = 0;
			var option:AQPollOption;
			var choices:Array = event.choices;
			this._tags.text = event.subject;
            this._question.init(event.questionID, event.question, false);
			this._date.text = event.date;
			//this._sender.text = event.senderName;
			this.id = event.id;
			this.type = event.category;
			
			this.choices = new Array();
			this.viewport = new Sprite();
			this.scroll.viewport = this.viewport;
			this.scroll.reform();
			
			if(this.type == 0){
				for (i = 0; i<choices.length; i++){
					option = new AQPollOption(choices[i]['id'], choices[i]['text'], false);
					option.includeRadioButton();
					option.addEventListener(AQPollOptionEvent.POLL_SELECTED, processPollSelected);
					this.viewport.addChild(option);
					this.choices.push(option);
				}
			}else if(this.type ==1){
				for (i = 0; i<choices.length; i++){
					option = new AQPollOption(choices[i]['id'], choices[i]['text'], false);
					
					option.includeCheckBox();
					option.addEventListener(AQPollOptionEvent.POLL_SELECTED, processPollSelected);
					this.viewport.addChild(option);
					this.choices.push(option);
					option.text = choices[i];
				}
			}else if(this.type == 2){
				for (i = 0; i<choices.length; i++){
					option = new AQPollOption(choices[i]['id'], choices[i]['text'], false);
					option.text = choices[i];
					option.addEventListener(AQPollOptionEvent.POLL_SELECTED, processPollSelected);
					this.viewport.addChild(option);
					this.choices.push(option);
				}
			}
			this.reformVieport();
			this.scroll.reform();
		}
		
		private function sendVote(event:MouseEvent = null):void{
			var i:int = 0;
			var votes:Array = new Array();
			var option:AQPollOption;
			if(type == 0){
				for (i = 0; i<this.choices.length; i++){
					option = AQPollOption(this.choices[i]);
					if(option.isRadioButtonSelected()){
						votes.push(option.optionID);
					}
				}
			}else if(type ==1){
				for (i = 0; i<this.choices.length; i++){
					option = AQPollOption(this.choices[i]);
					if(option.isCheckboxSelected()){
						votes.push(option.optionID);
					}
				}
			}else if(type == 2){
				for (i = 0; i<this.choices.length; i++){
					option = AQPollOption(this.choices[i]);
					votes.push(option.optionID);
				}
			}
			
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_SEND_OBJECT;
			data.type = FluidConstants.POLL_VOTE;
			data.id = this.id;
			data.reply = this.id;
			data.votes = votes;
			var fvne:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(fvne);
			
			FluidEventHandler.dispatchEvent(new WidgetEvent(WidgetEvent.DISPLAY_WORKSPACE));
		}
		private function processPollSelected(event:AQPollOptionEvent):void{
			//if has radio box, then turn all other choices off)
			if(this.type == 0){
				for(var i:int = 0; i<this.choices.length; i++){
					var option:AQPollOption = this.choices[i];
					option.selectRadioButton(false);
				}
			}
			event.pollOption.selectRadioButton(true);
		}
		private function reformVieport():void{
			var maxY:int = 0;
			for(var i:int = 0; i<this.viewport.numChildren; i++){
				var c:DisplayObject = this.viewport.getChildAt(i);
				c.y = maxY;
				maxY =maxY+c.height;
			}
		}
		

	}
}